<template>
    <div class="container">
        <div class="title">
            <span>修改</span>
            <span class="back" @click="back">
        <i class="iconfont icon-fanhui"></i> 返回
      </span>
        </div>
        <el-divider></el-divider>
        <el-form label-width="180px">
            <div class="wrap">
                <el-form-item label="">
                    <upload-imgs
                            ref="uploadEle"
                            :value="imgValue"
                            :rules="rules"
                            :multiple="true"
                            :min-num="1"
                            :max-num="1"
                    />
                </el-form-item>
                <el-form-item label="专属卡名称" prop="name">
                    <el-input
                            size="medium"
                            v-model="name"
                            placeholder="请填写专属卡名称"
                    ></el-input>
                </el-form-item>
                <el-form-item label="所属店面" prop="store_id">
                    <el-select v-model="store_id" clearable filterable placeholder="请选择店面">
                        <el-option
                                v-for="item in storeData"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="专属卡价格" prop="present_price">
                    <el-input
                            size="medium"
                            type="number"
                            v-model="present_price"
                            placeholder="请填写专属卡价格"
                    ></el-input>
                </el-form-item>
                <el-form-item label="团长，团员分享返现金额" prop="group_invite_cash">
                    <el-input
                            size="medium"
                            type="number"
                            v-model="group_invite_cash"
                            placeholder="团长，团员分享返现金额"
                    ></el-input>
                </el-form-item>
                <el-form-item label="新顾客下单返佣金额" prop="new_user_pay_cash">
                    <el-input
                            size="medium"
                            type="number"
                            v-model="new_user_pay_cash"
                            placeholder="新顾客下单返佣金额"
                    ></el-input>
                </el-form-item>
                <el-form-item label="结束时间(几天后)" prop="end_time">
                    <el-input
                            size="medium"
                            type="number"
                            v-model="end_time"
                            placeholder="结束时间(几天后)"
                    ></el-input>
                </el-form-item>
                <el-form-item label="产品详情">
                    <div class="lin-wrap">
                        <tinymce :defaultContent="desc" @change="change" />
                    </div>
                </el-form-item>
                <el-form-item class="submit">
                    <el-button type="primary" @click="updateData()">保 存</el-button>
                </el-form-item>
            </div>
        </el-form>
    </div>
</template>

<script>
import Store from '@/models/store'
import product from '@/models/product'
import UploadImgs from '@/components/base/upload-imgs'
import Tinymce from '@/components/base/tinymce'

export default {
  components: {
    UploadImgs,
    Tinymce,
  },
  props: {
    ProductId: {
      type: Number,
    },
  },
  data() {
    return {
      imgValue: [],
      storeData: [],
      name: '',
      store_id: '',
      present_price: '',
      group_invite_cash: '',
      new_user_pay_cash: '',
      end_time: '',
      desc: '',
      rules: {
        maxSize: 0.4,
      },
    }
  },
  mounted() {
    this.getStoreAll()
    this.getInfo()
  },
  methods: {
    async getStoreAll() {
      this.storeData = await Store.getStoreAll()
    },
    async getInfo() {
      const info = await product.getProductInfo(this.ProductId)
      this.imgValue = [
        {
          delete: false,
          id: info.id,
          imgId: info.img_id,
          display: info.img.url,
        },
      ]
      this.name = info.name
      this.store_id = info.store_id
      this.present_price = info.present_price
      this.group_invite_cash = info.group_invite_cash
      this.new_user_pay_cash = info.new_user_pay_cash
      this.end_time = info.end_time
      this.desc = info.desc
    },
    back() {
      this.$emit('editClose')
    },
    async updateData() {
      const self = this
      const imgValue = await this.$refs.uploadEle.getValue()
      const info = {
        id: self.ProductId,
        img_id: imgValue[0].imgId,
        name: self.name,
        store_id: self.store_id,
        present_price: self.present_price,
        group_invite_cash: self.group_invite_cash,
        new_user_pay_cash: self.new_user_pay_cash,
        end_time: self.end_time,
        desc: self.desc,
      }
      const res = await product.updateProductType(info)
      if (res.error_code === 0) {
        self.imgValue = []
        this.$message.success(`${res.msg}`)
        this.back()
      }
    },
    change(val) {
      this.desc = val
    },
  },
}
</script>

<style lang="scss" scoped>
    .el-divider--horizontal {
        margin: 0;
    }

    .container {
        .title {
            height: 59px;
            line-height: 59px;
            color: $parent-title-color;
            font-size: 16px;
            font-weight: 500;
            text-indent: 40px;

            .back {
                float: right;
                margin-right: 40px;
                cursor: pointer;
            }
        }

        .wrap {
            padding: 20px;
        }

        .submit {
            float: left;
        }
    }
</style>
